/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "sopcast.h"
#include "mypopupmenu.h"
#include "sys.h"
#include "config.h"

Sopcast::Sopcast (QWidget * parent, const char *name):
QVBox (parent, name)
{
  //popup
  popup = new QLabel (this, 0, WStyle_Customize | WType_Dialog | WStyle_NoBorder);	//WType_Popup);        
  popup->setFrameStyle (QFrame::Raised | QFrame::Panel);
//  popup->setAutoResize (TRUE);
//  popup->resize (150, 150);

  //popupmenu
  QPopupMenu *file = new QPopupMenu (this);
  QPopupMenu *config = new QPopupMenu (this);
  config->insertItem ("Config", this, SLOT (onconfigclicked ()));
  history = new QPopupMenu (this);
  connect (history, SIGNAL (activated (int)), this,
	   SLOT (onchoosehistory (int)));
  bookmark = new MyPopupMenu (this);
  bookmark->insertItem ("Bookmark", this, SLOT (onbookmarkclicked ()),
			CTRL + Key_B);
  bookmark->insertSeparator ();
  connect (bookmark, SIGNAL (activated (int)), this,
	   SLOT (onchoosebookmark (int)));
  QPopupMenu *help = new QPopupMenu (this);
  help->insertItem ("&About", this, SLOT (about ()), CTRL + Key_H);

  //menubar
  QMenuBar *m = new QMenuBar (this);
  m->setSeparator (QMenuBar::InWindowsStyle);
  m->insertItem ("&File", file);
  m->insertItem ("&Config", config);
  m->insertItem ("&History", history);
  m->insertItem ("&Bookmark", bookmark);
  m->insertItem ("&help", help);


  ///
  QHBox *hbox = new QHBox (this);
  buttonchannel = new QPushButton ("Channel", hbox);
  buttonchannel->setToggleButton (TRUE);
  editsearch = new QLineEdit ("", hbox);
  editsearch->setFocus ();
  file->insertItem ("Focus", editsearch, SLOT (setFocus ()), Key_Escape);
  file->insertItem ("Exit", qApp, SLOT (quit ()), CTRL + Key_Q);
  buttonsearch = new QPushButton ("Search", hbox);
  buttonexpand = new QPushButton ("Expand", hbox);

  listchannel = new QListView (this);
  listchannel->addColumn (tr ("Name"));
/*  listchannel->addColumn (tr ("Status"));
  listchannel->addColumn (tr ("Type"));
  listchannel->addColumn (tr ("Kbps"));
  listchannel->addColumn (tr ("Users"));
  listchannel->addColumn (tr ("Vists"));
  listchannel->addColumn (tr ("QualityS"));
  listchannel->addColumn (tr ("QualityC"));
  listchannel->addColumn (tr ("StarFrom"));*/
//  listchannel->addColumn (tr ("URL"));
//  listchannel->addColumn (tr ("Description"));
  listchannel->setRootIsDecorated (TRUE);

  statuschannel = new QStatusBar (this);
  statuschannel->setSizeGripEnabled (FALSE);

  hbox = new QHBox (this);
  buttonplayer = new QPushButton ("Player", hbox);
  QPushButton *buttonstop = new QPushButton ("Stop", hbox);

  sys = new Sys ();
  slidersound =
    new QSlider (0, 100, 10, sys->sound_read (), QSlider::Horizontal, hbox);

  buttonrecord = new QPushButton ("Record", hbox);

  statusbar = new QStatusBar (this);

/////vboxconfig
  vboxconfig = new Config (this, 0, WType_Dialog);

/////signal and slot  
  connect (slidersound, SIGNAL (valueChanged (int)), this,
	   SLOT (sound_write (int)));
  http = new QHttp ();
  connect (http, SIGNAL (done (bool)), SLOT (httpdone (bool)));
  connect (buttonchannel, SIGNAL (clicked ()), this, SLOT (fetchchannel ()));
  connect (buttonstop, SIGNAL (clicked ()), this,
	   SLOT (onbuttonstopclicked ()));
  connect (buttonplayer, SIGNAL (clicked ()), this,
	   SLOT (onbuttonplayerclicked ()));
  connect (buttonrecord, SIGNAL (clicked ()), this,
	   SLOT (onbuttonrecordclicked ()));
  connect (buttonexpand, SIGNAL (clicked ()), this,
	   SLOT (onbuttonexpandclicked ()));
  connect (buttonsearch, SIGNAL (clicked ()), this,
	   SLOT (onbuttonsearchclicked ()));
  connect (editsearch, SIGNAL (returnPressed ()), this,
	   SLOT (onbuttonsearchclicked ()));

  connect (listchannel, SIGNAL (doubleClicked (QListViewItem *)), this,
	   SLOT (onlistitemclicked (QListViewItem *)));
  connect (listchannel, SIGNAL (returnPressed (QListViewItem *)), this,
	   SLOT (onlistitemclicked (QListViewItem *)));
  connect (listchannel,
	   SIGNAL (rightButtonClicked (QListViewItem *, const QPoint &, int)),
	   this,
	   SLOT (onrightbuttonclicked
		 (QListViewItem *, const QPoint &, int)));
  connect (listchannel, SIGNAL (onItem (QListViewItem *)), this,
	   SLOT (onmousemoved (QListViewItem *)));


  sop = new QProcess (this);
  connect (sop, SIGNAL (readyReadStdout ()), this, SLOT (parsestdout ()));
  connect (sop, SIGNAL (processExited ()), this, SLOT (onsopexit ()));
  player = new QProcess (this);
  connect (player, SIGNAL (processExited ()), this, SLOT (onplayerexit ()));
  bufchannel = new QBuffer ();

  QTimer *timer = new QTimer (this);
  connect (timer, SIGNAL (timeout ()), this, SLOT (timerDone ()));
  timer->start (1000, FALSE);

////initialization
  fetchingchannel = FALSE;
  flaghook = FALSE;
  loadopenstate ();
  loadbookmark ();
  recordfp = new QFile ();
}


Sopcast::~Sopcast ()
{
  saveopenstate ();
  savebookmark ();
  sys->~Sys ();

}
